//
//  DistributeViewController.m
//  zhixinApp
//
//  Created by zhiguanzhl on 2017/11/11.
//  Copyright © 2017年 zhiguanzhl. All rights reserved.
//

#import "DistributeViewController.h"
#import <JavaScriptCore/JavaScriptCore.h>

@interface DistributeViewController ()<UIWebViewDelegate>
@property (nonatomic,strong)UIWebView *webView;
@property (nonatomic, assign) UIWebViewNavigationType navigationType;


@end

@implementation DistributeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   self.navigationController.navigationBarHidden = YES;
    self.view.backgroundColor = KWhiteColor;
    [self setWebView];

}
- (void)setWebView{
    
    UIWebView *webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 20, kSCREEN_WIDTH, kSCREEN_HEIGHT-20)];
    webView.backgroundColor = KWhiteColor;
    self.webView = webView;
    
     webView.delegate = self;
    [self.view addSubview:webView];
    
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"hello/content/report/report.html" relativeToURL:[[NSBundle mainBundle] bundleURL]]]];

    //下面的方法可以是的goBack方法，返回网页不刷新
    [[NSUserDefaults standardUserDefaults] setInteger:2 forKey: @"WebKitCacheModelPreferenceKey"];
    [[NSUserDefaults standardUserDefaults] setInteger:1 forKey: @"WebKitMediaPlaybackAllowsInline"];
    id webbView = [self.webView valueForKeyPath:@"_internal.browserView._webView"];
    id preferences = [webbView valueForKey:@"preferences"];
    [preferences performSelector:@selector(_postCacheModelChangedNotification)];
}


#pragma mark - UIWebViewDelegate


- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    
//    [self.webView reload];
    
   //拦截路径 NSLog(@"%@%@",request.URL.absoluteURL,request.URL.absoluteString);
    return YES;
}
- (void)webViewDidStartLoad:(UIWebView *)webView {
//    NSLog(@"fghjkl");
    
}


- (void)webViewDidFinishLoad:(UIWebView *)webView {
    
    @WeakObj(self);
    
    if (_navigationType == UIWebViewNavigationTypeBackForward) {
        
        _navigationType = 0;
    }
    
    JSContext *context = [self.webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    //定义好JS要调用的方法, share就是调用的share方法名
    
    context[@"sendInfoToJava"] = ^(NSString *name) { // 调用返回
        if ([name isEqualToString:@"back"]) {
            dispatch_async(dispatch_get_main_queue(), ^{
                if ([self.webView canGoBack]) {
                    //默认返回是刷新网页的
                    [self.webView goBack];
                    
                }else{
                    [self.view resignFirstResponder];
                    [self.navigationController popViewControllerAnimated:YES];
                }
            });
        }
        
        if ([name isEqualToString:@"close"]) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.navigationController popViewControllerAnimated:YES];
                
            });
        }
        
        if ([name isEqualToString:@"go"]) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.webView reload];
                [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"hello/content/report/report_check.html" relativeToURL:[[NSBundle mainBundle] bundleURL]]]];
            });
        }
        
        
        
        if ([name isEqualToString:@"alreadyChange"]) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.webView reload];
                [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"hello/content/report/report_check.html" relativeToURL:[[NSBundle mainBundle] bundleURL]]]];
            });
        }
        
        
    };
    
    NSString *companyId = [NSString stringWithFormat:@"%@",self.companyID];
    NSString *loginName = [DataCenter shareInstence].loginName;
    NSString *name = [Tool unChiveWithString:loginName];
    NSString *token = [[NSUserDefaults standardUserDefaults] valueForKey:@"userToken"];
    
    //native ->js(<=2参数)
    NSString *report = [selfWeak.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"showInfoFromIos(\"%@\",\"%@\")", companyId,loginName]];
    NSString *report1 = [selfWeak.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"showInfo(\"%@\",\"%@\")", name,token]];
    
    
    [selfWeak.webView stringByEvaluatingJavaScriptFromString:report];
    [selfWeak.webView stringByEvaluatingJavaScriptFromString:report1];
}




@end
